/**
 * Copyright 2008-2009 HAN.BIFANG
 *
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *
 */
package han.bifang.gwt.client.service.content;

import han.bifang.gwt.client.common.Content;
import han.bifang.gwt.client.common.ContentChangeListener;
import han.bifang.gwt.client.common.EntityConstants;
import han.bifang.gwt.client.common.GwtContext;
import han.bifang.gwt.client.common.RequestObject;
import han.bifang.gwt.client.common.ResponseObject;
import han.bifang.gwt.client.common.TreeNodeChangeListener;
import han.bifang.gwt.client.common.TreeNodeContentListener;
import han.bifang.gwt.client.common.entity.Service;
import han.bifang.gwt.client.common.entity.Version;
import han.bifang.gwt.client.communicate.BifangGWTServiceAsync;
import han.bifang.gwt.client.service.ServiceConstants;
import han.bifang.gwt.client.service.ServiceContentConstants;
import han.bifang.gwt.client.service.ServiceMessages;
import han.bifang.gwt.client.service.tree.ServiceDomainTreeItem;
import han.bifang.gwt.client.service.tree.ServiceVersionTree;
import han.bifang.gwt.client.service.tree.ServiceVersionTreeItem;

import java.util.ArrayList;
import java.util.HashMap;

import com.google.gwt.user.client.Window;
import com.google.gwt.user.client.rpc.AsyncCallback;
import com.google.gwt.user.client.ui.DecoratorPanel;
import com.google.gwt.user.client.ui.HorizontalPanel;
import com.google.gwt.user.client.ui.TreeItem;

/**
 * 
 * @Author Kent
 */
public class ServiceRootContent extends Content implements
		TreeNodeContentListener, ContentChangeListener {
	private TreeItem selectedItem = null;

	private DecoratorPanel panel = null;

	private ServiceMessages cons = null;

	private BifangGWTServiceAsync service = null;

	private GwtContext context = null;

	private TreeNodeChangeListener nodeListener = null;

	private HorizontalPanel hp;
	private String id;

	private Service ser;

	private DecoratorPanel treePanel;

	private AsyncCallback<ResponseObject> cb = new AsyncCallback<ResponseObject>() {
		public void onSuccess(ResponseObject res) {
			String code = res.getCode();
			if (!code.equals("0")) {
				Window.alert("There was an error: " + res.getMsg());
				return;
			}

			ArrayList ds = (ArrayList) res.getResponse();

			ser = (Service) ds.get(0);
			// ArrayList qos = (ArrayList) ds.get(1);
			// BusServiceRel r = (BusServiceRel) ds.get(2);
			initTree(ser);
			ServiceDetailContent sdc = new ServiceDetailContent(cons, service,
					context, ds, null);
			//sdc.onLoad();
			initServiceDetailContent(sdc);

			// TabPanel tab = new TabPanel();
			// tab.add(new ServiceBasicContent(cons, service, context, ser),
			// cons
			// .basic());
			// tab.add(new ServiceConfigContent(cons, service, context, qos),
			// cons
			// .config());
			// tab.add(new ServiceBusBindingContent(cons, service, context, r),
			// cons.bus());
			//
			// tab.selectTab(0);
			// tab.setWidth("100%");
			// VerticalPanel serviceContentPanel = new VerticalPanel();
			// serviceContentPanel.add(tab);
			// serviceContentPanel.add(new HTML("&nbsp;"));
			//
			// HorizontalPanel h1 = new HorizontalPanel();
			//
			// Button b = new Button(cons.addVersion());
			// b.addClickListener(new ClickListener() {
			//
			// public void onClick(Widget sender) {
			// context.put(ServiceConstants.SERVICE, ser);
			//
			// onContentChanged(ServiceContentConstants.SERVICE_ROOT,
			// ServiceContentConstants.CREATE_VERSION_BASIC, null);
			// }
			//
			// });
			//
			// h1.add(b);
			// h1.add(new HTML("&nbsp;&nbsp;"));
			//
			// b = new Button(cons.remove());
			// h1.add(b);
			// h1.add(new HTML("&nbsp;&nbsp;"));
			//
			// b = new Button(cons.ret());
			// h1.add(b);
			//
			// serviceContentPanel.add(h1);
			//

			panel.setWidget(sdc);

		}

		public void onFailure(Throwable caught) {
			Window.alert("There was an error: " + caught.toString());
		}
	};

	public void initTree(Service ser) {
		ServiceVersionTree tree = new ServiceVersionTree(cons, service,
				context, ser);

		tree.setTreeNodeContentListener(this);
		this.setTreeNodeChangeListener(tree);

		treePanel.add(tree);
	}

	public void initServiceDetailContent(ServiceDetailContent c) {
		c.setContentChangeListener(this);
	}

	public void setTreeNodeChangeListener(TreeNodeChangeListener l) {

		this.nodeListener = l;
	}

	public void onTreeNodeSelected(TreeItem item) {
		this.selectedItem = item;
		if (item instanceof ServiceDomainTreeItem) {
			ServiceDomainTreeItem i = (ServiceDomainTreeItem) item;
			if (i.getSubType().equals(ServiceConstants.SERVICE_DOMAIN_ROOT)) {
				ServiceRootMgmtContent src = new ServiceRootMgmtContent(cons,
						service, context);
				src.setContentChangeListener(this);
				this.panel.setWidget(src);
				return;
			}
			if (i.getSubType().equals(EntityConstants.TREE_APP_APP)) {
				AppContent src = new AppContent(cons, service, context, i
						.getKey());
				src.setContentChangeListener(this);
				this.panel.setWidget(src);
				context.put(ServiceConstants.APP_ID, i.getKey());
				return;
			}
		} else if (item instanceof ServiceVersionTreeItem) {
			ServiceVersionTreeItem i = (ServiceVersionTreeItem) item;
			if (i.getSubType().equals(ServiceConstants.SERVICE_VERSION)) {
				VersionRootContent vrc = new VersionRootContent(cons, service,
						context, i.getKey());
				vrc.setContentChangeListener(this);
				panel.setWidget(vrc);
				return;
			} else if (i.getSubType().equals(ServiceConstants.SERVICE_ROOT)) {
				ServiceDetailContent sdc = new ServiceDetailContent(cons,
						service, context, null, id);
				initServiceDetailContent(sdc);
				panel.setWidget(sdc);
				return;

			}
		}

	}

	public ServiceRootContent(ServiceMessages cons,
			BifangGWTServiceAsync service, GwtContext context, String id) {
		this.service = service;
		this.context = context;
		this.cons = cons;
		this.id = id;

		hp = new HorizontalPanel();
		hp.setWidth("100%");

		// hp.setHorizontalAlignment(HasHorizontalAlignment.ALIGN_LEFT);
		initWidget(hp);

	}

	public void onLoad() {
		treePanel = new DecoratorPanel();
		treePanel.setWidth("100%");

		hp.add(treePanel);

		panel = new DecoratorPanel();
		panel.setWidth("100%");
		hp.add(panel);
		hp.setCellWidth(treePanel, "25%");
		hp.setCellWidth(panel, "75%");
		RequestObject req = new RequestObject();
		req.setRequest(id);
		req
				.setAction("han.bifang.gwt.server.action.service.GetServiceDetailAction");
		service.call(req, cb);

	}

	public void onContentChanged(String source, String dest, HashMap props) {
//		if (source.equals(ServiceContentConstants.SERVICE_MGMT_ROOT)
//				&& dest.equals(ServiceContentConstants.CREATE_SERVICE_BASIC)) {
//			// remove the cached service
//			context.remove(ServiceConstants.SERVICE);
//			
//			
//			CreateServiceBasicContent src = new CreateServiceBasicContent(cons,
//					service, context);
//			src.setContentChangeListener(this);
//			panel.setWidget(src);
//			return;
//		}
//		if (source.equals(ServiceContentConstants.CREATE_SERVICE_BASIC)
//				&& dest.equals(ServiceContentConstants.CREATE_SERVICE_CONFIG)) {
//
//			CreateServiceConfigContent src = new CreateServiceConfigContent(
//					cons, context);
//			src.setContentChangeListener(this);
//			panel.setWidget(src);
//			return;
//		}
//		if (source.equals(ServiceContentConstants.CREATE_SERVICE_CONFIG)
//				&& dest.equals(ServiceContentConstants.CREATE_SERVICE_BASIC)) {
//
//			CreateServiceBasicContent src = new CreateServiceBasicContent(cons,
//					service, context);
//			src.setContentChangeListener(this);
//			panel.setWidget(src);
//			return;
//		}
//		if (source.equals(ServiceContentConstants.CREATE_SERVICE_CONFIG)
//				&& dest
//						.equals(ServiceContentConstants.CREATE_SERVICE_BUS_BINDING)) {
//
//			CreateServiceBusBindingContent src = new CreateServiceBusBindingContent(
//					cons, service, context);
//			src.setContentChangeListener(this);
//			panel.setWidget(src);
//			return;
//		}
//		if (source.equals(ServiceContentConstants.CREATE_SERVICE_BUS_BINDING)
//				&& dest.equals(ServiceContentConstants.CREATE_SERVICE_CONFIG)) {
//
//			CreateServiceConfigContent src = new CreateServiceConfigContent(
//					cons, context);
//			src.setContentChangeListener(this);
//			panel.setWidget(src);
//			return;
//		}
//
		if (source.equals(ServiceContentConstants.SERVICE_ROOT)
				&& dest.equals(ServiceContentConstants.CREATE_VERSION_BASIC)) {
			// clear context cached page
			context.remove(ServiceConstants.VERSION_CONFIG_PAGE);
			context.remove(ServiceConstants.VERSION);
			context.remove(ServiceConstants.VERSION_QOS); 
			CreateVersionBasicContent src = new CreateVersionBasicContent(cons,
					service, context);
			src.setContentChangeListener(this);
			panel.setWidget(src);
			return;
		}
		if (source.equals(ServiceContentConstants.CREATE_VERSION_BASIC)
				&& dest.equals(ServiceContentConstants.CREATE_VERSION_COMM)) {

			if (context.get(ServiceConstants.VERSION_CONFIG_PAGE) != null) {
				CreateVersionCommContent src = (CreateVersionCommContent) context
						.get(ServiceConstants.VERSION_CONFIG_PAGE);
				panel.setWidget(src);
			} else {
				CreateVersionCommContent src = new CreateVersionCommContent(
						cons, service, context);
				src.setContentChangeListener(this);
				panel.setWidget(src);
			}
			return;
		}
		if (source.equals(ServiceContentConstants.CREATE_VERSION_COMM)
				&& dest.equals(ServiceContentConstants.CREATE_VERSION_BASIC)) {

			CreateVersionBasicContent src = new CreateVersionBasicContent(cons,
					service, context);
			src.setContentChangeListener(this);
			panel.setWidget(src);
			return;
		}
		if (source.equals(ServiceContentConstants.CREATE_VERSION_COMM)
				&& dest.equals(ServiceContentConstants.CREATE_VERSION_CONFIG)) {
			// cache page
			context
					.put(ServiceConstants.VERSION_CONFIG_PAGE, panel
							.getWidget());

			CreateVersionConfigContent src = new CreateVersionConfigContent(
					cons, service, context);
			src.setContentChangeListener(this);
			panel.setWidget(src);
			return;
		}

		if (source.equals(ServiceContentConstants.CREATE_VERSION_CONFIG)
				&& dest.equals(ServiceContentConstants.CREATE_VERSION_COMM)) {

			if (context.get(ServiceConstants.VERSION_CONFIG_PAGE) != null) {
				CreateVersionCommContent src = (CreateVersionCommContent) context
						.get(ServiceConstants.VERSION_CONFIG_PAGE);
				panel.setWidget(src);
			}
			return;
		}

		if (source.equals(ServiceContentConstants.CREATE_VERSION_CONFIG)
				&& dest.equals(ServiceContentConstants.VERSION_ROOT)) {

			context.remove(ServiceConstants.VERSION_CONFIG_PAGE);
			context.remove(ServiceConstants.VERSION_QOS); 
			ServiceVersionTreeItem i = new ServiceVersionTreeItem();
			i.setHTML((String) props.get("v"));
			i.setSubType(ServiceConstants.SERVICE_VERSION);
			i.setKey((String) props.get("id"));

			nodeListener.onAddNode(i);
			// i.setSelected(true);

			VersionRootContent vrc = new VersionRootContent(cons, service,
					context, (String) props.get("id"));
			vrc.setContentChangeListener(this);
			panel.setWidget(vrc);

			return;
		}

		if (source.equals(ServiceContentConstants.VERSION_ROOT)
				&& dest.equals(ServiceContentConstants.MODIFY_VERSION_BASIC)) {
			context.remove(ServiceConstants.VERSION_CONFIG_PAGE);
			context.remove(ServiceConstants.VERSION);

			String id = (String) props.get("id");
			ModifyVersionBasicContent mvc = new ModifyVersionBasicContent(cons,
					service, context, id);
			mvc.setContentChangeListener(this);
			panel.setWidget(mvc);
			return;
		}

		if (source.equals(ServiceContentConstants.MODIFY_VERSION_BASIC)
				&& dest.equals(ServiceContentConstants.MODIFY_VERSION_COMM)) {
			if (context.get(ServiceConstants.VERSION_CONFIG_PAGE) != null) {
				ModifyVersionCommContent mvc = (ModifyVersionCommContent) context
						.get(ServiceConstants.VERSION_CONFIG_PAGE);
				panel.setWidget(mvc);
				return;
			}
			ModifyVersionCommContent mvc = new ModifyVersionCommContent(cons,
					service, context);
			mvc.setContentChangeListener(this);
			panel.setWidget(mvc);
			return;
		}

		if (source.equals(ServiceContentConstants.MODIFY_VERSION_COMM)
				&& dest.equals(ServiceContentConstants.MODIFY_VERSION_BASIC)) {
			Version v = (Version) context.get(ServiceConstants.VERSION);

			ModifyVersionBasicContent mvc = new ModifyVersionBasicContent(cons,
					service, context, v.getId());
			mvc.setContentChangeListener(this);
			panel.setWidget(mvc);
			return;
		}

		if (source.equals(ServiceContentConstants.MODIFY_VERSION_COMM)
				&& dest.equals(ServiceContentConstants.MODIFY_VERSION_CONFIG)) {

			ModifyVersionConfigContent mvc = new ModifyVersionConfigContent(
					cons, service, context);
			mvc.setContentChangeListener(this);
			context
					.put(ServiceConstants.VERSION_CONFIG_PAGE, panel
							.getWidget());
			panel.setWidget(mvc);
			return;
		}
		if (source.equals(ServiceContentConstants.MODIFY_VERSION_CONFIG)
				&& dest.equals(ServiceContentConstants.MODIFY_VERSION_COMM)) {

			if (context.get(ServiceConstants.VERSION_CONFIG_PAGE) != null) {
				ModifyVersionCommContent mvc = (ModifyVersionCommContent) context
						.get(ServiceConstants.VERSION_CONFIG_PAGE);
				panel.setWidget(mvc);
			}

			return;
		}

		if (source.equals(ServiceContentConstants.MODIFY_VERSION_CONFIG)
				&& dest.equals(ServiceContentConstants.VERSION_ROOT)) {

			context.remove(ServiceConstants.VERSION_CONFIG_PAGE);

			// i.setSelected(true);

			VersionRootContent vrc = new VersionRootContent(cons, service,
					context, (String) props.get("id"));
			vrc.setContentChangeListener(this);
			panel.setWidget(vrc);

			return;
		}

		if (source.equals(ServiceContentConstants.REMOVE_VERSION)
				&& dest.equals(ServiceContentConstants.SERVICE_ROOT)) {

			ServiceDetailContent sdc = new ServiceDetailContent(cons, service,
					context, null, id);
			nodeListener.onRemoveNode(selectedItem);
			initServiceDetailContent(sdc);
			panel.setWidget(sdc);

			return;
		}
		if (source.equals(ServiceContentConstants.VERSION_ROOT)
				&& dest.equals(ServiceContentConstants.REMOVE_VERSION)) {

			RemoveVersionContent vrc = new RemoveVersionContent(cons, service,
					 (String) props.get("id"),(String) props.get("version"));
			vrc.setContentChangeListener(this);
			panel.setWidget(vrc);

			return;
		}
		if (source.equals(ServiceContentConstants.REMOVE_VERSION)
				&& dest.equals(ServiceContentConstants.VERSION_ROOT)) {

			VersionRootContent vrc = new VersionRootContent(cons, service,
					context, (String) props.get("id"));
			vrc.setContentChangeListener(this);
			panel.setWidget(vrc);

			return;
		}
		
		if (source.equals(ServiceContentConstants.SERVICE_DETAIL)
				&& dest.equals(ServiceContentConstants.REMOVE_SERVICE)) {
			
			RemoveServiceContent vrc = new RemoveServiceContent(cons, service,
				 context);
			vrc.setContentChangeListener(this);
			panel.setWidget(vrc);
			
			return;
		}
		if (source.equals(ServiceContentConstants.REMOVE_SERVICE)
				&& dest.equals(ServiceContentConstants.SERVICE_ROOT)) {

			this.listener.onContentChanged(
					ServiceContentConstants.REMOVE_SERVICE,
					ServiceContentConstants.SERVICE_ROOT, props);

			return;
		}

		if (source.equals(ServiceContentConstants.REMOVE_SERVICE)
				&& dest.equals(ServiceContentConstants.SEARCH_RESULT)) {

			this.listener.onContentChanged(
					ServiceContentConstants.REMOVE_SERVICE,
					ServiceContentConstants.SEARCH_RESULT, null);

			return;
		}
		
		if (source.equals(ServiceContentConstants.SERVICE_DETAIL)
				&& dest.equals(ServiceContentConstants.SEARCH_RESULT)) {
			
			this.listener.onContentChanged(
					ServiceContentConstants.SERVICE_DETAIL,
					ServiceContentConstants.SEARCH_RESULT, null);

			return;
		}

	}

}
